<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ClassBaseInfo;
use App\Services\Admin\Level;
use App\Services\Admin\User;
use App\Services\Admin\UserVip;
use App\Http\Requests\Admin\UserController\doRule;
use App\Services\Api\GoodsServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function index(GoodsServices $goodsServices)
    {
        $user_lists = User::lists()->paginate(15);

        foreach ($user_lists as $v) {
            $user_goods_integral = $goodsServices->user_goods_integral($v->id);

            $v->user_goods_integral = $user_goods_integral;
        }

        $result = [
            'result' => $user_lists,
            'sear_value' => request()->sear_value ?? '',
            'headhunting_commission_level' => Level::index()->get()
        ];

        return view('admin.user.index', $result);
    }

    public function edit($id)
    {
        $result = [
            'result' => User::info($id),
            'id' => $id
        ];

        return view('admin.user.edit', $result);
    }

    public function update_expire_time()
    {
        User::update_expire_time(request()->all());
        return self::success();
    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $info_user_number = User::info_user_number([['user_number', $input['user_number']]]);

            if (!empty($info_user_number)) {
                if ($info_user_number->id != $input['id']) {
                    return self::fail('该会员编号已有会员使用');
                }
            }

            User::edit($input['id'], $input);

            return self::success([]);
        }
    }

    //积分记录
    public function points_record($id)
    {
        $result = [
            'user_lists' => User::integral_change_record($id)->select('chage_type', 'reason', 'use_integral', 'created_at')->orderBy('created_at', 'desc')->paginate(15),
            'start_time' => request()->get('start_time'),
            'end_time' => request()->get('end_time'),
            'chage_type' => request()->get('chage_type'),
            'id' => $id,
            'user_info' => User::info($id)
        ];

        return view('admin.user.points_record', $result);
    }

    //操作积分
    public function record_edit($id)
    {
        $result = [
            'id' => $id,
        ];

        return view('admin.user.record_edit', $result);
    }

    //操作积分
    public function record_do()
    {
        $input = request()->all();

        User::record_do($input['id'], $input['reason'], $input['type'], $input['integral']);

        return self::success([]);
    }

    public function edit_rank()
    {
        $input = request()->all();

        $user_array = explode('_', $input['name']);

        $user_id = end($user_array);

        User::edit_rank($user_id, $input['rank_id']);

        return self::success([]);
    }

    public function user_import()
    {
        $result = [
            'result' => User::lists()->get(),
            'headhunting_commission_level' => Level::index()->get()
        ];

        return view('admin.user.down', $result);
    }

    //vip
    public function vip($id)
    {
        $user_vip = (new UserVip);

        $user_vip_info = $user_vip->user_vip($id);

        foreach ($user_vip_info as &$v){
            $vip_maturity_string = explode(" ", $v->vip_maturity);
            $v->vip_maturity = $vip_maturity_string[0]??date("Y-m-d");
        }

        $result = [
            'user_vip' => $user_vip_info,
            'vip_lists' => $user_vip->vip_lists(),
            'user_id' => $id,
        ];

        return view('admin.user.vip', $result);
    }

    public function user_update_vip_expire_time()
    {
        UserVip::vip_maturity(request()->all());
        return self::success();
    }

    public function user_update_vip_activate_time()
    {
        UserVip::vip_activate(request()->all());
        return self::success();
    }

    //vip 使用記錄
    public function vip_use_info($user_id, $vip_id)
    {
        $user_vip = (new UserVip);

        $user_vip_info = $user_vip->vip_use_info($user_id, $vip_id);

        $result = [
            'user_vip_info' => $user_vip_info,
            'user_id' => $user_id,
            'start_time' => request()->get('start_time'),
            'end_time' => request()->get('end_time'),
            'class_name' => request()->get('class_name'),
        ];

        return view('admin.user.vip_use_info', $result);
    }

    //vip 詳細
    public function vip_info($user_id, $vip_id)
    {
        $user_vip = (new UserVip);

        $user_vip_info = $user_vip->user_vip_info($user_id, $vip_id);

        foreach ($user_vip_info as $v){
            if(!empty($v->class_base_info)) {
                $class_base_info = $v->class_base_info->where("id", $v->class_info_id)->with(['class_base', 'teacher', 'class_time'])->first();
                $v->class_base_info = $class_base_info;
            }else{
                $v->class_base_info = null;
            }
        }

        $result = [
            'user_vip_info' => $user_vip_info,
            'user_id' => $user_id
        ];

        return view('admin.user.vip_info', $result);
    }

    public function vip_del($user_id, $vip_id)
    {
        (new UserVip)->user_vip_del($user_id, $vip_id);

        return self::success([]);
    }

    public function vip_do(Request $request, UserVip $user_vip)
    {
        if ($request->ajax()) {

            DB::beginTransaction();

            $input = $request->all();

            try {

                $user_vip->vip_edit($input['vip_id'], $input['user_id']);

                DB::commit();

            } catch (\Exception $e) {

                //接收异常处理并回滚
                DB::rollBack();

                return $this->fail($e->getMessage());

            }

            return self::success([]);

        }
    }
}
